package com.tianhui.consignortwofei.mvp.ui.activity.hetong.presenter;

import android.content.Context;

import com.fgs.common.CommonResponse;
import com.fgs.common.entity.CommonDataListInfo;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.api.hetong.HuRequest;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.hetong.contract.ContractListContract;


import java.util.List;

import io.reactivex.Observable;

/**
 * @author fgs
 * @date 2020/4/1
 * @description
 **/
public class ContractListPresenter extends ContractListContract.Presenter {
    private Context mContext;
    private OrderModel mModel;

    public ContractListPresenter(Context context) {
        mContext = context;
        mModel = new OrderModel();
    }


    @Override
    public void requestOrderList(Observable observable, boolean isDialog, HuRequest huRequest, int page) {

//            Map<String, Object> params = new HashMap<>();
//            params.put("limit", "10");
//            params.put("isblack", "1");
//            params.put("page", page + "");
//            huRequest.getFengMianImage(params, new BaseObserver<String>() {
//                @Override
//                public void onNext(String json) {
//                    try {
//                        String json1 = json;
//
//                        if (TextUtils.isEmpty(json)) {
//                            ToastUtils.longToast("请求失败");
//                            return;
//                        }
//                        HeTongTwoBean bean = JSON.parseObject(json, HeTongTwoBean.class);
//                        if (bean == null) {
//                            ToastUtils.longToast("返回数据空");
//                            return;
//                        }
//                        if (bean.getCode() == HttpConstants.RESPONSE_CODE_200) {
//
//                            heTongListResult(bean);
//
//                        } else {
//                            ToastUtils.longToast(bean.getMsg());
//                        }
//                    } catch (Exception e) {
//                        Log.i("fei", e.toString());
//                    }
//                }
//            });


        mModel.orderListModel(mContext, observable, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<CommonDataListInfo>>() {
            @Override
            public void onNext(CommonResponse<CommonDataListInfo> commonResponse) {
                if (null != getView()) {
                    processOrderListResult(commonResponse);
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (null != getView()) {
                    getView().setMsg(e.message);
                    getView().stopRefresh();
                }
            }
        });
    }

//    @Override
//    public void requestSourceList(Map<String, String> map, boolean isDialog) {
//        mModel.sourceListModel(mContext, map, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<OrderData>>() {
//            @Override
//            public void onNext(CommonResponse<OrderData> baseResponse) {
//                if (null != getView()) {
//                    processOrderListResult(baseResponse);
//                }
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                if (null != getView()) {
//                    getView().setMsg(e.message);
//                    getView().stopRefresh();
//                }
//            }
//        });
//    }

//    @Override
//    public void requestDispatchList(Map<String, String> map, boolean isDialog) {
//        mModel.dispatchListModel(mContext, map, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<OrderData>>() {
//            @Override
//            public void onNext(CommonResponse<OrderData> baseResponse) {
//                if (null != getView()) {
//                    processOrderListResult(baseResponse);
//                }
//            }
//
//            @Override
//            public void onError(ExceptionHandle.ResponseThrowable e) {
//                if (null != getView()) {
//                    getView().setMsg(e.message);
//                    getView().stopRefresh();
//                }
//            }
//        });
//    }

    private void processOrderListResult(CommonResponse<CommonDataListInfo> baseResponse) {
        if (baseResponse.getCode() == 20000) {
            CommonDataListInfo data = baseResponse.getData();
            getView().setPageInfo(data.current, data.pages);
            List<OrderInfo> list = data.records;
            if (list != null && list.size() > 0) {
                getView().showDataList(list);
            } else {
                getView().showEmptyView();
            }
        } else {
            getView().setMsg(baseResponse.getMsg());
            getView().stopRefresh();
        }
    }



//    private void heTongListResult(HeTongTwoBean baseResponse) {
//        if (baseResponse.getCode() == 20000) {
//            HeTongTwoBean.HeTongListInfo data = baseResponse.getData();
//            getView().setPageInfo(data.current, data.pages);
////            List<OrderInfo> list = data.records;
////            if (list != null && list.size() > 0) {
////                getView().showDataList(list);
////            } else {
////                getView().showEmptyView();
////            }
//        } else {
//            getView().setMsg(baseResponse.getMsg());
//            getView().stopRefresh();
//        }
//    }

}
